﻿using DevTeam;
using System.Collections.Generic;
using System.Data;
using System.Linq;

//TODO: width hangi en?
namespace ConfExpert.Forms.ImportWizards
{
    public partial class Destex_LF_Tip3 : XITopCekiListesi
    {        
        int YuklemeId;
        public Destex_LF_Tip3(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            Init("LF Tip3 Çeki Listesi", true);
            this.FileDone += this_FileDone;
        }

        void this_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                Dictionary<string, int> indexes = new Dictionary<string, int>();
                Dictionary<string, string> pairs = new Dictionary<string, string>();
                pairs.Add("Article", "Color No.");
                //pairs.Add("LotNo", "LotNo");
                pairs.Add("VendorRollId", "Roll No.");
                pairs.Add("Qty", "Roll length(M)");
                pairs.Add("Width", "Actual width(CM)");
                 

                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    DataRow dr0 = dt.Rows[0];
                    string colName = dr0[colIndex].ToStr();

                    foreach (KeyValuePair<string, string> pair in pairs)
                    {
                        if (colName.ToLower().Replace(" ","") == pair.Value.ToLower().Replace(" ",""))
                        {
                            indexes.Add(pair.Key, colIndex);
                            pairs.Remove(pair.Key);
                            break;
                        }
                    }
                }

                if (pairs.Count > 0)
                {
                    string col = pairs.ElementAt(0).Value;
                    ShowErrorMessage(dt.TableName + " çalışma sayfasında " + col + " isimli sütun bulunamadı !");
                    return;
                }
                   
                
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    string article = dr[indexes["Article"]].ToStr();
                    if (Article.Select("Article='" + article + "'").Length == 0)
                    {
                        DataRow drArticle = Article.NewRow();
                        drArticle["Article"] = article;
                        Article.Rows.Add(drArticle);
                    }

                    DataRow drRoll = Roll.NewRow();
                    drRoll["Article"] = article;
                    drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                    drRoll["Qty"] = dr[indexes["Qty"]];
                    //drRoll["LotNo"] = dr[indexes["LotNo"]];
                    drRoll["Width"] = dr[indexes["Width"]];
                    Roll.Rows.Add(drRoll);
                }

              
            }
        }


    }
}


